package com.hl233.device.controller;

import com.hl233.common.feign.SystemClient;
import com.hl233.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 张振强15065206763
 * @create 2024-09-10
 */
@RestController
@RequestMapping("/device")
public class DeviceController {
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private SystemClient systemClient;

    @GetMapping("/queryAll")
    public String queryAll() {
        List<ServiceInstance> list =discoveryClient.getInstances("hl233-system");
        System.out.println("这个里面放的是什么"+list.get(0).getUri());
        ResponseEntity<List<Map>> response=
                restTemplate.exchange(
                list.get(0).getUri() + "/system/queryAll",
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<Map>>(){});
        List<Map> list1 = response.getBody();
        for (Map res:list1){
            System.out.println("查看数据"+res);
        }


        return "ok了";
    }

    @GetMapping("/addBackData")
    public String addBackData() {
        Map map = new HashMap();
        map.put("name","测试feign");
        map.put("createtime","2024-09-03 08:02:33");
        Long userId = SecurityUtils.getUserId();
        System.out.println("通过fegin拿到userid"+userId);

        String res = systemClient.addBackData(map);
        System.out.println("huodejiguo:"+res);
        return res+"xxl";
    }

    @GetMapping("/getbyid1")
    public String getbyid1(@RequestHeader(value = "truth",required = false) String truth) {
        System.out.println("拿到这个头truth"+truth);
        String res = systemClient.getById1(1);
        System.out.println("huodejiguo:"+res);
        return res+"xxl1";
    }

    @GetMapping("/getbyid2")
    public String getbyid2() {
        String res = systemClient.getById2(2);
        System.out.println("huodejiguo:"+res);
        return res+"xxl2";
    }


}
